package rsvp.util;


/**
 * build up average  
 * 
 */
public class Average  {
	
	private double m_lfMin;
	private double m_lfMax;
	private double m_lfSum = 0;
	private int m_iCounter = 0;
	
	/**
	 * add a new value
	 * 
	 * @param lfValue
	 */
	public void add(double lfValue) {

		if (0 == m_iCounter)
			m_lfMin = m_lfMax = lfValue;
		if (m_lfMin > lfValue)
			m_lfMin = lfValue;
		if (m_lfMax < lfValue)
			m_lfMax = lfValue;
				
		
		
		m_lfSum += lfValue;
		m_iCounter++;
	}
	
	/**
	 * @return average so far
	 */
	public double getAverage() {
		return m_lfSum / m_iCounter;
	}
	
	/**
	 * reset counters
	 */
	public void clear() {
		m_lfSum = 0;
		m_iCounter = 0;
	}		
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "" + getAverage();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toStringDetails() {
		return "iCounter=" + m_iCounter 
		+ ", lfMin=" + m_lfMin 
		+ ", lfMax=" + m_lfMax 
		+ ", average=" + getAverage()
		+ ", sum=" + m_lfSum;
	}	
}

